﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using XFControls.XFPanels.XFPanelItems;
using FBEntities;
using System.IO;
using System.Reflection;
using FBEntities.FBObjects.User;
using XFItemControls.XFPanels.XFPanelItems;
using FBEntities.FBConnections.User;

namespace XFControls.XFPanels
{
    public enum XFPanelHomeItems
    {
        NewsFeed,
        Profile,
        Friends,
        Inbox,
        Chat,
        Requests,
        Events,
        Photos,
        Notes,
        None
    }
    public partial class XFPanelHome : XFPanelBase
    {
        private int _bottomHeight;
        private Rectangle _blackout;
        private int _imageHeight;

        public XFPanelHome()
        {
            InitializeComponent();
            _bottomHeight = Screen.PrimaryScreen.WorkingArea.Height / 8;

            Width = Screen.PrimaryScreen.WorkingArea.Width;
            Height = Screen.PrimaryScreen.WorkingArea.Height;
            _imageHeight = Height - _bottomHeight;
            deSelectItem();
            PanelName = "Home";
        }

        public override void PanelPaint(Graphics g)
        {
            if (Parent != null && _imageHeight != Parent.Height - _bottomHeight)
                _imageHeight = Parent.Height - _bottomHeight; ;

            try
            {
                Rectangle recDest = new Rectangle(0, 0, Width, _imageHeight);
                this.DrawUI("XFPanelHome", g, recDest);
            }
            catch
            {
                using (Pen p = new Pen(Color.LightGray))
                {
                    g.DrawLine(p, Width / 3, 0, Width / 3, _imageHeight);
                    g.DrawLine(p, (Width * 2) / 3, 0, (Width * 2) / 3, _imageHeight);

                    g.DrawLine(p, 0, _imageHeight / 3, Screen.PrimaryScreen.WorkingArea.Width, _imageHeight / 3);
                    g.DrawLine(p, 0, (_imageHeight * 2) / 3, Screen.PrimaryScreen.WorkingArea.Width, (_imageHeight * 2) / 3);

                    g.DrawLine(p, 0, _imageHeight, Width, _imageHeight);
                }
            }

            if (_blackout.Width > 0 && _blackout.Height > 0)
            {
                XFControlUtils.DrawBlackout(g, _blackout);
            }
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            switch (_curState)
            {
                case States.Click:
                    selectItem(e.X, e.Y);
                    Invalidate();
                    break;
            }
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            switch (_curState)
            {
                case States.Scroll:
                    deSelectItem();
                    Invalidate();
                    break;
            }
        }

        private void selectItem(int x, int y)
        {
            int itmWidth = Width / 3;
            int itmHeight = _imageHeight / 3;

            switch (getSelectedItem(x, y))
            {
                case XFPanelHomeItems.NewsFeed:
                    _blackout = new Rectangle(0, 0, itmWidth, itmHeight);
                    break;
                case XFPanelHomeItems.Profile:
                    _blackout = new Rectangle(itmWidth, 0, itmWidth, itmHeight);
                    break;
                case XFPanelHomeItems.Friends:
                    _blackout = new Rectangle(itmWidth * 2, 0, itmWidth, itmHeight);
                    break;

                case XFPanelHomeItems.Inbox:
                    _blackout = new Rectangle(0, itmHeight, itmWidth, itmHeight);
                    break;
                case XFPanelHomeItems.Chat:
                    _blackout = new Rectangle(itmWidth, itmHeight, itmWidth, itmHeight);
                    break;
                case XFPanelHomeItems.Requests:
                    _blackout = new Rectangle(itmWidth * 2, itmHeight, itmWidth, itmHeight);
                    break;

                case XFPanelHomeItems.Events:
                    _blackout = new Rectangle(0, itmHeight * 2, itmWidth, itmHeight);
                    break;
                case XFPanelHomeItems.Photos:
                    _blackout = new Rectangle(itmWidth, itmHeight * 2, itmWidth, itmHeight);
                    break;
                case XFPanelHomeItems.Notes:
                    _blackout = new Rectangle(itmWidth * 2, itmHeight * 2, itmWidth, itmHeight);
                    break;
            }
        }

        private void deSelectItem()
        {
            _blackout = new Rectangle(0, 0, 0, 0);
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            switch (_curState)
            {
                case States.Click:
                    var itm = getSelectedItem(e.X, e.Y);

                    switch (itm)
                    {
                        case XFPanelHomeItems.NewsFeed:
                            setupNewsFeed();
                            break;
                        case XFPanelHomeItems.Profile:
                            setupProfile();
                            break;
                        case XFPanelHomeItems.Friends:
                            setupFriends();
                            break;

                        case XFPanelHomeItems.Inbox:
                            setupInbox();
                            break;
                        case XFPanelHomeItems.Chat:
                            setupChat();
                            break;
                        case XFPanelHomeItems.Requests:
                            setupRequests();
                            break;

                        case XFPanelHomeItems.Events:
                            setupEvents();
                            break;
                        case XFPanelHomeItems.Photos:
                            setupPhotos();
                            break;
                        case XFPanelHomeItems.Notes:
                            setupNotes();
                            break;
                    }
                    break;
            }

            base.OnMouseUp(e);
        }

        private void setupNewsFeed()
        {
            _curState = States.SlideLeft;

            XFPanelList panel = new XFPanelList();

            XFWorkerLoadNewsFeeds worker = new XFWorkerLoadNewsFeeds(XFItemControlUtils.Me.Me);
            XFItemLoading loading = new XFItemLoading(worker);

            loading.WorkFinished += (o, e) =>
                {
                    FBPerson parsedPers = null;
                    if (e.Worker is XFWorkerLoadNewsFeeds)
                        parsedPers = ((XFWorkerLoadNewsFeeds)e.Worker).ParsedPersion;

                    var par = ((XFItemLoading)o).Parent as XFPanelList;

                    if (!par.IsDisposed)
                        par.Invoke(new Action(() =>
                            {
                                par.ClearItems();
                                foreach (var p in parsedPers.Home.data)
                                {
                                    XFItemPost post = new XFItemPost(p);
                                    par.Add(post);
                                }
                            }));
                };

            SetRightPanel(panel);
            panel.ShowAlphaBar(false);
            panel.ShowScrollbar(true);
            panel.ScrollbarColor = Color.LightSlateGray;

            XFButtonHome home = new XFButtonHome();

            XFPanelContainer parent = (XFPanelContainer)Parent;
            parent.SetHeaderLeftButton(home);

            panel.Add(loading);
            FBHome homeCache = new FBHome();
            homeCache.ParseCached(XFItemControlUtils.Me.Me.id);

            foreach (var p in homeCache.data)
            {
                XFItemPost post = new XFItemPost(p);
                panel.Add(post);
            }

            panel.PanelName = "News Feed";
        }

        private void setupProfile()
        {
            _curState = States.SlideLeft;

            XFPanelList panel = XFItemControlUtils.SetupProfile(XFItemControlUtils.Me.Me);

            SetRightPanel(panel);
            panel.ShowAlphaBar(false);
            panel.ShowScrollbar(true);

            XFButtonHome home = new XFButtonHome();

            XFPanelContainer parent = (XFPanelContainer)Parent;
            parent.SetHeaderLeftButton(home);

            panel.PanelName = "Profile";
        }

        private void setupFriends()
        {
            _curState = States.SlideLeft;

            FBDataSource ds = XFItemControlUtils.Me == null ? new FBDataSource("False") : XFItemControlUtils.Me;
            XFPanelList panel = new XFPanelList();

            var worker = new XFWorkerLoadFriends(ds.Me);
            var loading = new XFItemLoading(worker);

            loading.WorkFinished += (o, e) =>
            {
                FBDataSource dsInner = XFItemControlUtils.Me == null ? new FBDataSource("False") : XFItemControlUtils.Me;
                XFItemLoading l = o as XFItemLoading;
                XFItemControlUtils.AddFriends((XFPanelList)l.Parent, dsInner);
            };

            panel.Add(loading);
            panel.ScrollbarColor = Color.LightSlateGray;

            SetRightPanel(panel);
            panel.ShowAlphaBar(true);
            panel.ShowScrollbar(true);

            XFButtonHome home = new XFButtonHome();

            XFPanelContainer parent = (XFPanelContainer)Parent;
            parent.SetHeaderLeftButton(home);

            loading.DoWork();
            panel.PanelName = "Friends";
        }

        private void setupInbox()
        {
            _curState = States.SlideLeft;

            FBDataSource ds = XFItemControlUtils.Me == null ? new FBDataSource("False") : XFItemControlUtils.Me;
            XFPanelList panel = new XFPanelList();
            FBInbox inbox = new FBInbox();

            var worker = new XFWorkerLoadInbox(ds.Me);
            var loading = new XFItemLoading(worker);

            loading.WorkFinished += (o, e) =>
            {
                FBDataSource dsInner = XFItemControlUtils.Me == null ? new FBDataSource("False") : XFItemControlUtils.Me;
                XFItemLoading l = o as XFItemLoading;
                XFItemControlUtils.AddMailItems((XFPanelList)l.Parent, dsInner);
            };

            panel.Add(new XFItemComposeMessage());
            panel.Add(loading);
            panel.ScrollbarColor = Color.LightSlateGray;
            inbox.ParseCached(ds.Me.id);

            foreach (var item in inbox.data)
            {
                panel.Add(new XFItemInboxMessage(item));
            }

            SetRightPanel(panel);
            panel.ShowAlphaBar(false);
            panel.ShowScrollbar(true);

            XFButtonHome home = new XFButtonHome();

            XFPanelContainer parent = (XFPanelContainer)Parent;
            parent.SetHeaderLeftButton(home);

            loading.DoWork();
            panel.PanelName = "Inbox";
        }

        private void setupChat()
        {
            _curState = States.SlideLeft;

            XFPanelList panel = new XFPanelList();

            panel.Add("Coming Soon");

            SetRightPanel(panel);
            panel.ShowAlphaBar(false);
            panel.ShowScrollbar(true);

            XFButtonHome home = new XFButtonHome();

            XFPanelContainer parent = (XFPanelContainer)Parent;
            parent.SetHeaderLeftButton(home);

            panel.PanelName = "Chat";
        }

        private void setupRequests()
        {
            _curState = States.SlideLeft;

            XFPanelList panel = new XFPanelList();

            panel.Add("Coming Soon");

            SetRightPanel(panel);
            panel.ShowAlphaBar(false);
            panel.ShowScrollbar(true);

            XFButtonHome home = new XFButtonHome();

            XFPanelContainer parent = (XFPanelContainer)Parent;
            parent.SetHeaderLeftButton(home);

            panel.PanelName = "Requests";
        }

        private void setupEvents()
        {
            _curState = States.SlideLeft;

            XFPanelList panel = new XFPanelList();

            panel.Add("Coming Soon");

            SetRightPanel(panel);
            panel.ShowAlphaBar(false);
            panel.ShowScrollbar(true);

            XFButtonHome home = new XFButtonHome();

            XFPanelContainer parent = (XFPanelContainer)Parent;
            parent.SetHeaderLeftButton(home);

            panel.PanelName = "Events";
        }

        private void setupPhotos()
        {
            _curState = States.SlideLeft;

            XFPanelList panel = new XFPanelList();

            panel.Add("Coming Soon");

            SetRightPanel(panel);
            panel.ShowAlphaBar(false);
            panel.ShowScrollbar(true);

            XFButtonHome home = new XFButtonHome();

            XFPanelContainer parent = (XFPanelContainer)Parent;
            parent.SetHeaderLeftButton(home);

            panel.PanelName = "Photos";
        }

        private void setupNotes()
        {
            _curState = States.SlideLeft;

            XFPanelList panel = new XFPanelList();

            panel.Add("Coming Soon");

            SetRightPanel(panel);
            panel.ShowAlphaBar(false);
            panel.ShowScrollbar(true);

            XFButtonHome home = new XFButtonHome();

            XFPanelContainer parent = (XFPanelContainer)Parent;
            parent.SetHeaderLeftButton(home);

            panel.PanelName = "Notes";
        }

        private XFPanelHomeItems getSelectedItem(int x, int y)
        {
            if (x < (Width / 3))
            {
                if (y < (_imageHeight / 3))
                {
                    return XFPanelHomeItems.NewsFeed;
                }
                else if (y < ((_imageHeight * 2) / 3))
                {
                    return XFPanelHomeItems.Inbox;
                }
                else
                {
                    return XFPanelHomeItems.Events;
                }
            }
            else if (x < ((Width * 2) / 3))
            {
                if (y < (_imageHeight / 3))
                {
                    return XFPanelHomeItems.Profile;
                }
                else if (y < ((_imageHeight * 2) / 3))
                {
                    return XFPanelHomeItems.Chat;
                }
                else
                {
                    return XFPanelHomeItems.Photos;
                }
            }
            else
            {
                if (y < (_imageHeight / 3))
                {
                    return XFPanelHomeItems.Friends;
                }
                else if (y < ((_imageHeight * 2) / 3))
                {
                    return XFPanelHomeItems.Requests;
                }
                else
                {
                    return XFPanelHomeItems.Notes;
                }
            }
        }
    }
}
